//
//  LMJRadioListController.m
//  EPA
//
//  Created by lanou3g on 16/4/22.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LMJRadioListController.h"
#import "LMJRadioListCell.h"
#import "LMJRedioController.h"
#import "LMJRedioListModel.h"
#import "LMJNetWorkRequestManager.h"
#import "URLHeaderDefine.h"
#import "LMJRadioLiatModel.h"
#import "LORefresh.h"
#import "LMJRedioListModel.h"

@interface LMJRadioListController ()
@property (nonatomic,strong) NSMutableArray *radioArray;
@property (nonatomic,strong) NSString *start;
@property (nonatomic,strong)LORefresh *headerRefresh;
@end

@implementation LMJRadioListController

-(void)dealloc {
    //下拉
//    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    //footer
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电台列表";
    
   //注册
    [self.tableView registerNib:[UINib nibWithNibName:@"LMJRadioListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.radioArray = [NSMutableArray new];
    self.start = @"0";
    [self  loadDataFromServerStart:self.start];
#pragma mark - 电台列表上下拉刷新
    __weak typeof(self) weakSelf = self;
//    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
//        //移除
//        [weakSelf.radioArray removeAllObjects];
//        [weakSelf.tableView reloadData];
//        //网络请示
//        [weakSelf loadDataFromServerStart:@"0"];
//    }];
//    //gif图刷新
//    self.headerRefresh = [self.tableView gifHeader];
//    [self.headerRefresh setGifName:@"wangnima"];
    //上提
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.radioArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",count];
        //网络请求
        [weakSelf loadDataFromServerStart:weakSelf.start];
    }];

  
   }

//网络请求,解析
- (void)loadDataFromServerStart:(NSString *)start {
    [LMJNetWorkRequestManager requestURL:[NSURL URLWithString:RADIOMLISTORE_URL] Method:LMJ_POST Params:@{@"auth":@"XZU7RH7m1861DC8Z8H8HvkTJxQMGoPLGO9zo4XDA0cWP22NdFSh9d7fo",@"client":@"1",@"deviceid":@"6D4DD967-5EB2-40E2-A202-37E64F3BEA31",@"limit":@"9",@"start":start,@"version":@"3.0.6"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDict);
        NSMutableDictionary *dataDict = jsonDict[@"data"];
        NSArray *alllist = dataDict[@"list"];
        for (NSMutableDictionary *dict in alllist) {
            LMJRadioLiatModel *model = [LMJRadioLiatModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.radioArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            //手动停止调用刷新,加载
//            [[self.tableView gifHeader]endRefreshing];
            [[self.tableView defaultFooter]endRefreshing];
        });
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.radioArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LMJRadioListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.radioArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 145;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LMJRedioController *redioVC = [[LMJRedioController alloc]init];
    LMJRadioLiatModel *model = [LMJRadioLiatModel new];
  model = self.radioArray[indexPath.row];
    redioVC.RadioID = model.radioid;
    

     [self.navigationController pushViewController:redioVC animated:YES];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    
}


@end
